#! /bin/sh
# -*- tcl -*-

# If $LANG is a utf8 one, exp_sending \xFF sends \303\277 to xvi
# so we set LANG=C to circumvent that. (Un)setting LANG in TCL doesn't work,
# presumably because tclsh or expect checks $LANG's setting on startup.
# This means you can only run this test file as a shell script, not with tclsh.
#
# Was https://github.com/martinwguy/xvi/issues/20

# The next line is executed by /bin/sh, but not tcl \
LANG=C exec tclsh "$0" ${1+"$@"}
if { ! [info exists env(LANG)] || $env(LANG) != "C" } {
    send_error "Couldn't set LANG to C".
    fail 99
}

#
# Test file for showing an ^ with the cursor on the ^ when you've pressed ^V
# and it's waiting for the literal character.
#

source scripts/term
start_vi

# Tests begin

# Check it started up OK
test 100 ""		1 0 [list "" "~"]

# 1. In INSERT mode

# A literal ^V at the start of the line
test 11 "a[ctrl V]"		1 0 [list "^" "~"]
test 12 [ctrl V]		1 2 [list "^V" "~"]
exp_send [esc]

# In the middle of a line
test 13 "0Daabcd[esc]0ll"	1 2 [list "abcd" "~"]  ;# Cursor on the c
test 14 "i[ctrl V]"		1 2 [list "ab^cd" "~"]
		# Test Ctrl-V + regular character
test 15 "X"			1 3 [list "abXcd" "~"]
exp_send [esc]

# 2. In REPLACE mode

# 2a. Single character
test 16 "0Daabcde[esc]0ll" 1 2 [list "abcde" "~"]	;# Cursor on the c
# nvi fails to display ^ when replacing a char with with ^V"
test 17 "r[ctrl V]"		1 2 [list "ab^de" "~"]
# Something that should shunt the line up by 1 char
# When sitting on a ^V, xvi and vim put the cursor on the ^ but
# nvi and classic vi put it on the V.
set c18 2
if { $vi == "nvi" || $vi == "vi" } { set c18 3 }
test 18 "[ctrl V]"		1 $c18 [list "ab^Vde" "~"]

# 2b. Multiple characters
test 19 "0Daabcdefg[esc]0ll"	1 2 [list "abcdefg" "~"]  ;# Cursor on the c
test 20 "RX[ctrl V]"		1 3 [list "abX^efg" "~"]
test 21 "Y"			1 4 [list "abXYefg" "~"]
# Only xvi, lacking UTF-8, displays top-bit-set characters thus
if { $vi == "xvi" } {
 test 22 "[ctrl V]"		1 4 [list "abXY^fg" "~"]
 test 23 "\xFF"		1 8 [list "abXY\\377fg" "~"]
 test 24 "Z"			1 9 [list "abXY\\377Zg" "~"]
}
exp_send [esc]

# 3. On the command line
ctest 25 ":[ctrl V]"		1 ":^"
ctest 26 "[ctrl V]"		3 ":^V"
ctest 27 "[ctrl V]"		3 ":^V^"
ctest 28 "A"			4 ":^VA"
if { $vi == "xvi" } {
 ctest 29 "[ctrl V]"		4 ":^VA^"
 ctest 30 "\xFF"		8 ":^VA\\377"
 ctest 31 "\b\b\b"		1 ":"
} else {
 # nvi doesn't clear the status line unless you leave and re-enter command mode
 ctest 31 "\b\b\b:"		1 ":"
}
# Send left arrow and check that it works in the middle of the line.
# Only vi has command-line editing
if { $vi == "xvi" || $vi == "vim" } {
ctest 41 "abcd[left][left][ctrl V]" 3 ":ab^cd"
ctest 42 "[ctrl V]"		5 ":ab^Vcd"
exp_send "\b\b\b\b:"
}

# Check interaction with insert maps. literal characters shouldn't be
# mapped by ":map!" mappings.

exp_send "map! X Y\r"
exp_send "1GdG"
test 51 "iX[ctrl V]X"		1 2 [list "YX"]	 ;# INSERT mode
test 52 "[esc]0Cabc[esc]0"	1 0 [list "abc"] ;# reset test pattern
# nvi fails this because it doesn't put replaced characters through insert maps
test 53 "rXlr[ctrl V]X"		1 1 [list "YXc"] ;# 1-char REPLACE mode
test 54 "0Cabc[esc]0"		1 0 [list "abc"] ;# reset test pattern
test 55 "RX[ctrl V]X"		1 2 [list "YXc"] ;# multi-char REPLACE mode
exp_send "[esc]"

# Need to check that inserting the same text again works (i.e. that
# Flexbuf Insbuff is set correctly and regurgitates correctly).

stop_vi

exit 0
